<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1、求最大值
        function getMax(arr) {
            // 填入代码
            let max = Math.max(...arr)
            return max
        }

        let arr = [1, 6, 2, 8, 11, 5]
        let max = getMax(arr)
        console.log(max)

        // 2、求和
        function getSum(arr) {
            // 填入代码
            let sum = arr.reduce((c, t) => c + t, 0)
            return sum
        }
        let arr2 = [3, 9, 9, 2, 7, 5]
        let sum2 = getSum(arr2)
        console.log(sum2)

        // 3. 封装函数，求得传入数组⾥⼤于 5 的和 [4, 9, 6, 90, 3, 11]
        function getLargeSum(arr) {
            let res = arr.filter(item => item > 5)
            let sum = res.reduce((c, t) => c + t, 0)
            return sum
        }
        let arr3 = [4, 9, 6, 90, 3, 11]
        let sum3 = getLargeSum(arr3)
        console.log(sum3)

        // 4. 对数组，按照价格进行升序排序
        const arr4 = [
            { id: 1, name: '茶杯', price: 18 },
            { id: 2, name: '电视机', price: 2000 },
            { id: 3, name: '牙刷', price: 8 },
            { id: 4, name: '电脑', price: 5000 },
        ]
        // const eitherSort = (arr = []) => {
        //     const sorter = (a, b) => {
        //         return +a.price - +b.price
        //     }
        //     arr.sort(sorter)
        // }
        // eitherSort(arr4)
        // console.log(arr4)
        // arr4.sort((a, b) => {
        //     if (a.price < b.price || a.price == b.price) {
        //         return -1 // a小 往前移
        //     } else if (a.price > b.price || a.price == b.price) {
        //         return 1 // b大 往后移
        //     } else {
        //         return 0 // 不改变
        //     }
        // })
        arr4.sort(function (a, b) {
            return a.price - b.price
        })
        console.log(arr4)

        // 5. 将学生数组中的所有姓名，存入成一个新数组，并过滤掉小强 => 得到 ['小明', '小丽', '小美']
        const arr5 = [
            { id: 1, name: '小强', score: 18 },
            { id: 2, name: '小明', score: 2000 },
            { id: 3, name: '小丽', score: 8 },
            { id: 4, name: '小美', score: 5000 },
        ]
        const res = arr5.filter(item => item.name != '小强')
        console.log(res)
    </script>
</body>

</html>